package org.study.dsm.mybatisplus.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.study.dsm.mybatisplus.mapper.DataSourceInfoMapper;
import org.study.dsm.mybatisplus.model.DataSourceInfo;

import java.util.List;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年04月27日
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class DataSourceApplicationRunner implements ApplicationRunner {
    private final DataSourceInfoMapper dataSourceInfoMapper;
    private final DynamicDataSource dynamicDataSource;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<DataSourceInfo> dataSourceInfoList = dataSourceInfoMapper.selectList(null);
        if (CollectionUtils.isNotEmpty(dataSourceInfoList)) {
            // 数据源操作，也可以加入不同的数据源类型
            for (DataSourceInfo dataSourceInfo : dataSourceInfoList) {
                dynamicDataSource.createDataSourceWithCheck(dataSourceInfo);
            }
        }
    }
}
